import serial
import threading
from operation.logging import Logging
from PyQt5.QtCore import *
from serial_operation.serial_write import WriteThreadPneumatic
from serial_operation.serial_write import WriteThreadArduino
from serial_operation.serial_read import ReadThreadArduino


class SerialConnect(QObject):

    def __init__(self):
        super().__init__()
        self.read_arduino = ReadThreadArduino()

    def connect_pneumatic_serial(self, port):
        try:
            self.pneumatic_serial = serial.Serial(
                port=port,
                parity=serial.PARITY_NONE,
                stopbits=serial.STOPBITS_ONE,
                bytesize=serial.EIGHTBITS,
                baudrate=4800,
                write_timeout=0)
            if self.pneumatic_serial.isOpen():
                self.pneumatic_serial.flushInput()
                Logging.logger.info('pneumatic serial open success')
                return True
        except serial.SerialException as es:
            Logging.logger.exception(es)
            return False
        except Exception as e:
            Logging.logger.exception(e)
            return False

    def connect_arduino_serial(self, port):
        try:
            self.arduino_serial = serial.Serial(
                port=port,
                parity=serial.PARITY_NONE,
                stopbits=serial.STOPBITS_ONE,
                bytesize=serial.EIGHTBITS,
                baudrate=115200,
                writeTimeout=0)
            if self.arduino_serial.isOpen():
                self.arduino_serial.flushInput()
                read_thread = threading.Thread(target=self.read_arduino.run, args=(self.arduino_serial,),daemon=True)
                read_thread.start()
                Logging.logger.info('arduino serial open success')
                return True
        except serial.SerialException as es:
            Logging.logger.exception(es)
            return False
        except Exception as e:
            Logging.logger.exception(e)
            return False

    def serial_close(self, k):
        try:
            if k == 0:
                self.arduino_serial.close()
            elif k == 1:
                self.pneumatic_serial.close()
        except Exception as e:
            Logging.logger.exception(e)

    def pneumatic_send(self, index):
        try:
            if self.pneumatic_serial.isOpen():
                data = ID.pressure1_close
                if index == 0:
                    data = ID.pressure1_close
                elif index == 1:
                    data = ID.pressure2_close
                elif index == 2:
                    data = ID.positive_pressure
                elif index == 3:
                    data = ID.negative_pressure
                write_thread = WriteThreadPneumatic(self.pneumatic_serial, data)
                write_thread.start()
                return True
            return False
        except Exception as e:
            Logging.logger.exception(e)
            return False

    def arduino_send(self, data):
        try:
            if self.arduino_serial.isOpen():
                write_thread = WriteThreadArduino(self.arduino_serial, data)
                write_thread.start()
        except Exception as e:
            Logging.logger.exception(e)


class ID:
    pressure1_close = bytes([0x03])
    pressure2_close = bytes([0x07])
    positive_pressure = bytes([0x01])
    negative_pressure = bytes([0x06])
